function replace (arr: number[], dst: number, subs: number) {
  let j = 0
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === dst) {
      arr[j++] = subs
      ++i
    }
    while (i < arr.length && arr[i] === dst) {
      ++i
    }
    if (i < arr.length) {
      arr[j++] = arr[i]
    }
  }
  return arr.slice(0, j)
}

console.log(
  replace(
    [1, 2, 3, 4, 3, 3, 3, 5, 6, 3, 3, 7, 3, 3, 3, 8, 9, 3, 3, 3, 3, 3, 3],
    3,
    0
  )
)

let a = [1, 2, 3]
a.splice(0, 1, 5)
console.log(a)
